package models

import (
	"time"

	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"

	"gorm.io/plugin/soft_delete"
)

// 线上活动
type OnlineEvent struct {
	ID                 string                `json:"id" gorm:"primaryKey;column:id;autoIncrement;type:bigint;comment:唯一ID"`
	Name               string                `json:"name" gorm:"column:name;type:string;size:40;comment:活动名称"`
	StartTime          int64                 `json:"start_time" gorm:"column:start_time;type:bigint;comment:开始时间"`
	EndTime            int64                 `json:"end_time" gorm:"column:end_time;type:bigint;comment:结束时间"`
	Status             int                   `json:"status" gorm:"column:status;type:int;size:8;comment:状态,1-进行中 2-未开始 3-已结束"`
	IsDraw             int                   `json:"is_draw" gorm:"column:is_draw;type:int;size:8;comment:是否开奖过,1-是 2-否"`
	RequirePoints      int64                 `json:"require_points" gorm:"column:require_points;type:int;default:10;comment:参与积分"`
	RequireSupporters  int64                 `json:"require_supporters" gorm:"column:require_supporters;type:int;default:3;comment:助力人数"`
	ExpressFee         float64               `json:"express_fee" gorm:"column:express_fee;type:decimal(6,2);default:0.00;comment:运费"`
	Image              string                `json:"image" gorm:"column:image;type:string;size:100;comment:活动图片"`
	GoodsID            string                `json:"goods_id" gorm:"column:goods_id;type:bigint;comment:商品id"`
	LimitedFree        int64                 `json:"limited_free" gorm:"column:limited_free;type:bigint;comment:限免数量"`
	PurchaseMethod     int                   `json:"purchase_method" gorm:"column:purchase_method;type:int;size:8;comment:购买方式,1-无 2-直接购买 3-拔草"`
	GoodsLink          string                `json:"goods_link" gorm:"column:goods_link;type:text"`
	SelectedUser       string                `json:"selected_user,omitempty" gorm:"column:selected_user;type:text;comment:内定中奖用户"`
	SelectedPuppetUser string                `json:"selected_puppet_user,omitempty" gorm:"column:selected_puppet_user;type:text;comment:内定中奖马甲用户"`
	JoinedUser         int64                 `json:"joined_user" gorm:"column:joined_user;type:int;size:64;default:0;comment:已申请人数"`
	BaseJoinedUser     int64                 `json:"base_joined_user,omitempty" gorm:"column:base_joined_user;type:bigint;comment:已申请人数基数"`
	Description        string                `json:"description,omitempty" gorm:"column:description;type:text;comment:商品描述"`
	CreatedAt          int64                 `json:"created_at" gorm:"column:created_at;autoCreateTime;type:int;size:64;index:idx_createdAt"`
	UpdatedAt          int64                 `json:"updated_at" gorm:"column:updated_at;type:int;size:64"`
	DeletedAt          soft_delete.DeletedAt `json:"deleted_at" gorm:"column:deleted_at;type:bigint;default:0;comment:删除时间"`
	Top                int                   `json:"top" gorm:"column:top;type:int;size:32;default:0;comment:置顶"`
	Sort               int                   `json:"sort" gorm:"column:sort;type:int;size:32;default:0;comment:排序"`
	Visible            int                   `json:"visible" gorm:"column:visible;type:int;size:8;default:1;comment:是否可见,1-可见，2-不可见"`
	CheckGenderType    int                   `json:"check_gender_type" gorm:"column:check_gender_type;type:int;size:8;default:3;comment:校验性别类型：1-仅限男 2-仅限女 3-不校验 "`
}

// TableName 表名
func (OnlineEvent) TableName() string {
	return "online_event"
}

// BeforeCreate 自增id
func (m *OnlineEvent) BeforeCreate(tx *gorm.DB) (err error) {
	if m.ID != "" {
		return nil
	}
	// 用户id为空时，自动添加用户id
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	m.ID = uuid
	return nil
}

func (m *OnlineEvent) AfterFind(tx *gorm.DB) (err error) {
	if m.Status != 3 {
		//这里是基于以前代码的逻辑，老逻辑中，开奖会设置一个Status=3，所以设置过后就不用以时间来判断了
		now := time.Now()
		nowTS := now.Unix()
		if nowTS >= m.StartTime && nowTS <= m.EndTime {
			m.Status = 1 // 进行中
		} else if nowTS < m.StartTime {
			m.Status = 2 // 未开始
		} else if nowTS > m.EndTime {
			m.Status = 3 // 已结束
		}
	}

	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &OnlineEvent{})
}
